// pages/common/login/telLogin.js
let api = require("../../../requests/api.js");
let util = require('../../../utils/util.js');
let cache = require('../../../utils/cache.js');

const app = getApp();

Page({

  /**
   * Page initial data
   */
  data: {
    disabled: false,
    code: '获取验证码',
    phoneNumber: "",
    phoneCode: "",
    fromNewUser: false
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
    new app.WeToast();

    console.log('fromNewUser:' + options.newUser)
    //判断是否是从新人逻辑过来
    if (options.newUser == 'true') {
      console.log('fromNewUser:' + options.newUser)
      this.setData({
        fromNewUser: true
      })
    }
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  },

  // 获取手机验证码
  requestCode: function() {
    var params = {
      "mobile": this.data.phoneNumber
    };
    api.post_sendLoginCaptcha(params,
      (data) => {
        wx.hideNavigationBarLoading()
        if (data.resCode == '0') {
          console.log("获取验证码")
        }
      },
      (error) => {
        wx.hideNavigationBarLoading()
        if (util.alertNetworkError(error)) {
          that.wetoast.toast({
            title: error.resMsg,
          })
        }
        console.log(error)
      },
      (data) => {
        wx.hideNavigationBarLoading()
      }
    )
  },
  // 登录
  requestLogin: function() {
    var that = this;
    var params = {
      "mobile": that.data.phoneNumber,
      "verCode": that.data.phoneCode

    };
    api.post_loginByCaptcha(params,
      (data) => {
        wx.hideNavigationBarLoading()
        if (data.resCode == '0') {
          wx.setStorage({
            key: 'endUser',
            data: data.data
          })
          cache.endUser = data.data
          //裁剪头像
          var res = wx.getSystemInfoSync()
          let pixelRatio = res.pixelRatio
          let windowWidth = res.windowWidth
          if (typeof cache.endUser.avatar == 'string') {
            let clipUrl = util.clipimage(cache.endUser.avatar + "", util.rpx2px(125))
            cache.endUser.avatar = clipUrl
          }
          wx.showToast({
            title: '登录成功',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                if (that.data.fromNewUser) {
                  app.globalData.getCoupon = true
                  wx.switchTab({
                    url: '/pages/home/home',
                  })
                } else {
                  var pages = getCurrentPages();
                  if (pages.length > 1) {
                    wx.navigateBack({
                      delta: 2
                    })
                  } else {
                    wx.switchTab({
                      url: '/' + app.globalData.route,
                    })
                  }
                }
              }, 1500);
            }
          })
        }
      },
      (error) => {
        wx.hideNavigationBarLoading()
        if (util.alertNetworkError(error)) {
          that.wetoast.toast({
            title: error.resMsg,
          })
        }
        console.log(error)
      },
      (data) => {
        wx.hideNavigationBarLoading()
      }
    )
  },

  goGetCode: function() {
    var that = this;

    let number = that.data.phoneNumber
    if (!number) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    } else if (!util.checkMobile(number)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return
    }
    that.requestCode()
    var time = 60;
    that.setData({
      code: '60s',
      disabled: true
    })
    var Interval = setInterval(function() {
      time--;
      if (time > 0) {
        that.setData({
          code: time + 's'
        })
      } else {
        clearInterval(Interval);
        that.setData({
          code: '获取验证码',
          disabled: false
        })
      }
    }, 1000)

  },
  loginClick: function() {
    var that = this;
    let number = that.data.phoneNumber
    let code = that.data.phoneCode
    if (!number) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    } else if (!util.checkMobile(number)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return
    } else if (!code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    that.requestLogin()
  },

  xieyiClick: function() {
    wx.navigateTo({
      url: '../login/userAgreement',
    })
  },
  // 输入手机号
  fromPhone: function(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  //输入验证码
  fromCode: function(e) {
    this.setData({
      phoneCode: e.detail.value
    })
  }
})